package clienteguiview;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import clientes.controle.clienteControle;
import clientes.exceptions.Erro_Cpf;
import clientes.exceptions.Erro_Endereco;
import clientes.exceptions.Erro_Nome;

public class FrmNovoCliente extends JFrame {

	private JPanel contentPane;
	private JTextField textFieldNome;
	private JLabel lblCpf;
	private JTextField textFieldCpf;
	private JLabel lblBairro;
	private JTextField textFieldBairro;
	private JLabel lblCep;
	private JTextField textFieldCep;
	private JLabel lblComplemento;
	private JTextField textFieldComplemento;
	private JLabel lblLogradouro;
	private JTextField textFieldLogradouro;
	private JTextField textFieldNumero;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FrmNovoCliente frame = new FrmNovoCliente();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FrmNovoCliente() {
		setResizable(false);
		setTitle("Cadastro do Cliente");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 614, 330);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		JLabel lblNome = new JLabel("Nome : ");
		lblNome.setBounds(29, 31, 46, 14);
		contentPane.add(lblNome);
		
		textFieldNome = new JTextField();
		textFieldNome.setBounds(72, 28, 350, 20);
		contentPane.add(textFieldNome);
		textFieldNome.setColumns(10);
		
		lblCpf = new JLabel("Cpf : ");
		lblCpf.setBounds(29, 72, 46, 14);
		contentPane.add(lblCpf);
		
		textFieldCpf = new JTextField();
		textFieldCpf.setColumns(10);
		textFieldCpf.setBounds(72, 69, 173, 20);
		contentPane.add(textFieldCpf);
		
		lblBairro = new JLabel("Bairro : ");
		lblBairro.setBounds(29, 137, 38, 14);
		contentPane.add(lblBairro);
		
		textFieldBairro = new JTextField();
		textFieldBairro.setColumns(10);
		textFieldBairro.setBounds(72, 134, 240, 20);
		contentPane.add(textFieldBairro);
		
		lblCep = new JLabel("Cep : ");
		lblCep.setBounds(29, 172, 46, 14);
		contentPane.add(lblCep);
		
		textFieldCep = new JTextField();
		textFieldCep.setColumns(10);
		textFieldCep.setBounds(72, 169, 240, 20);
		contentPane.add(textFieldCep);
		
		lblComplemento = new JLabel("Complemento ");
		lblComplemento.setBounds(322, 137, 75, 14);
		contentPane.add(lblComplemento);
		
		textFieldComplemento = new JTextField();
		textFieldComplemento.setColumns(10);
		textFieldComplemento.setBounds(393, 134, 168, 20);
		contentPane.add(textFieldComplemento);
		
		lblLogradouro = new JLabel("Logradouro:");
		lblLogradouro.setBounds(29, 106, 64, 20);
		contentPane.add(lblLogradouro);
		
		textFieldLogradouro = new JTextField();
		textFieldLogradouro.setBounds(103, 106, 214, 20);
		contentPane.add(textFieldLogradouro);
		textFieldLogradouro.setColumns(10);
		
		JButton btnSalvar = new JButton("Salvar");
		btnSalvar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				try{
			
				
				new clienteControle().cadastrarCliente(textFieldNome.getText(), textFieldCpf.getText());
				new clienteControle().cadastrarEndereco(textFieldBairro.getText(), textFieldCep.getText(), textFieldComplemento.getText(),textFieldLogradouro.getText(),textFieldNumero.getText());
				
				JOptionPane.showMessageDialog(null, "Cadastro Realizado", "Informativo", JOptionPane.INFORMATION_MESSAGE);
				
				textFieldNome.setText("");
				textFieldCpf.setText("");
				textFieldLogradouro.setText("");
				textFieldNumero.setText("");
				textFieldCep.setText("");
				textFieldComplemento.setText("");
				textFieldBairro.setText("");
				
				
				} catch(Erro_Nome e1) {
					JOptionPane.showMessageDialog(null, e1.getMessage(), "erro", JOptionPane.ERROR_MESSAGE);
				}
			      catch (Erro_Cpf e2){
					JOptionPane.showMessageDialog(null, e2.getMessage(), "erro", JOptionPane.ERROR_MESSAGE);
				} catch (Erro_Endereco e3) {
					JOptionPane.showMessageDialog(null, e3.getMessage(), "erro", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnSalvar.setBounds(393, 226, 89, 23);
		contentPane.add(btnSalvar);
		
		JLabel lblNumero = new JLabel("Numero");
		lblNumero.setBounds(332, 109, 65, 14);
		contentPane.add(lblNumero);
		
		textFieldNumero = new JTextField();
		textFieldNumero.setBounds(408, 106, 75, 20);
		contentPane.add(textFieldNumero);
		textFieldNumero.setColumns(10);
	}
}
